import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/*
*   getServletContext() 获取servlet上下文对象,
*       一个web应用程序只有一个servlet上下文对象
*       在路径上相当于打开了target文件夹下面web应用的文件夹(与WEB-INF同级)，
*       读取文件就默认从这个文件夹开始，写相对路径
*       主要用途：
*       1.获取资源文件( 以此项目resources文件夹下的 my.properties文件为例)
*           InputStream is = new FileInputStream( this.getServletContext().getRealPath("/WEB-INF/classes/my.properties"));
*           InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/my.properties");
*       2.管理共享数据(由于所有的Servlet共享一个ServletContext对象)
*           1.存储数据
*               this.getServletContext().setAttribute("key","value");
*           2.获取数据
*               this.getServletContext().getAttribute("key");
* */
@WebServlet(value = "/ServletContextDemo01")
public class ServletContextDemo01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext();
        String realPath = this.getServletContext().getRealPath("/WEB-INF/classes/my.properties");
        InputStream is = new FileInputStream(realPath);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
